//
//  HBIncomeListDetailCtrl.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/7.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBIncomeListDetailCtrl.h"
#import "HBGetIncomeByDateInfo.h"
#import "HBGetIncomeByDateItemInfo.h"
#import "HBGetIncomeByDateParam.h"
#import "HBGetIncomeByDateParser.h"

@interface HBIncomeListDetailCtrl ()

@property (nonatomic, strong) NSArray *subAry;

@property (nonatomic, strong) UILabel *time;
@property (nonatomic, strong) UILabel *totalIncome;
@property (nonatomic, strong) UILabel *bjIncome;
@property (nonatomic, strong) UILabel *tyIncome;
@property (nonatomic, strong) UILabel *jxIncome;

@property (nonatomic, strong) HBGetIncomeByDateInfo *incomeInfo;

@end

@implementation HBIncomeListDetailCtrl

#pragma mark - system

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)buildLayout
{
    [super buildLayout];
    self.title = @"收益明细";
    self.view.backgroundColor = [LCCommon getColor:@"e1e1e1"];
    self.subAry = @[@"收益",@"本金收益",@"体验金收益",@"加息收益"];
    [self createHeaderView];
    [self createCustomView];
    [self loadDataFirst];
    [self requestToGetIncomeByDate];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
}

#pragma mark - private

- (void)createHeaderView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    bgView.backgroundColor = [LCCommon getColor:@"cfcfcf"];
    [self.view addSubview:bgView];

    self.time = [self createLableWithTextColor:[LCCommon getColor:@"666666"] font:[UIFont systemFontOfSize:12.0f] text:@"2015-11-30 收益（元）" textAlignment:NSTextAlignmentLeft frame:CGRectMake(15, 0, SCREEN_WIDTH-30, 35) superView:bgView];
}

- (void)createCustomView
{
    float xLeft = 23;
    float widthLeft = 100;
    float height = 50;
    float xRight = SCREEN_WIDTH-15-200;
    float widthRight = 200;
    
    for (int i=0;i<self.subAry.count;i++)
    {
        
        
//        UILabel *titleLable =
        [self createLableWithTextColor:[LCCommon getColor:@"888888"] font:[UIFont systemFontOfSize:16.0f] text:self.subAry[i] textAlignment:NSTextAlignmentLeft frame:CGRectMake(xLeft, 35+i*height, widthLeft, height) superView:self.view];
        UILabel *valueLable = [self createLableWithTextColor:[LCCommon getColor:@"666666"] font:[UIFont systemFontOfSize:16.0f] text:@"132111.00" textAlignment:NSTextAlignmentRight frame:CGRectMake(xRight, 35+i*height, widthRight, height) superView:self.view];
        
        [self createSepratorLineWithFrame:CGRectMake(15, 35+(i+1)*height-(1/([UIScreen mainScreen].scale)), SCREEN_WIDTH-30, (1/([UIScreen mainScreen].scale))) backgroundColor:[LCCommon getColor:@"cccccc"] superView:self.view];
        
        if (i==0)
        {
            self.totalIncome = valueLable;
        }
        else if (i==1)
        {
            self.bjIncome = valueLable;
        }
        else if (i==2)
        {
            self.tyIncome = valueLable;
        }
        else
        {
            self.jxIncome = valueLable;
        }
    }
    
    
}

- (UILabel *)createLableWithTextColor:(UIColor *)color font:(UIFont *)font text:(NSString *)text textAlignment:(NSTextAlignment)textAlignment frame:(CGRect)frame superView:(UIView *)superView
{
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.textColor = color;
    lable.font = font;
    lable.text = text;
    lable.textAlignment = textAlignment;
    [superView addSubview:lable];
    return lable;
}

- (UIView *)createSepratorLineWithFrame:(CGRect)frame backgroundColor:(UIColor *)bgColor superView:(UIView *)superView
{
    UIView *sepratorLine = [[UIView alloc]initWithFrame:frame];
    sepratorLine.backgroundColor = bgColor;
    [superView addSubview:sepratorLine];
    return sepratorLine;
}

- (void)loadDataFirst
{
    self.time.text = [NSString stringWithFormat:@"%@ 收益（元）",self.incomeDate];
    self.totalIncome.text = self.incomeMoney;
    self.bjIncome.text = @"0.00";
    self.tyIncome.text = @"0.00";
    self.jxIncome.text = @"0.00";
}

- (void)reloadData
{
    self.time.text = [NSString stringWithFormat:@"%@ 收益（元）",self.incomeDate];
    self.totalIncome.text = self.incomeMoney;
    for (HBGetIncomeByDateItemInfo *itemInfo in self.incomeInfo.dataAry)
    {
        switch ([itemInfo.incomeType integerValue])
        {
            case 0:
                self.bjIncome.text = itemInfo.incomeMoney;
                break;
            case 1:
                self.tyIncome.text = itemInfo.incomeMoney;
                break;
            case 2:
                self.jxIncome.text = itemInfo.incomeMoney;
                break;
            default:
                break;
        }
    }
}


#pragma mark -
- (void)requestToGetIncomeByDate
{
    [CustomLoadingView showHudToView:self.view];
    HBGetIncomeByDateParam *param = [[HBGetIncomeByDateParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    param.incomeDate = self.incomeDate;
    HBGetIncomeByDateParser *parser = [[HBGetIncomeByDateParser alloc]init];
    [NetCenter getDataFromURL:Method_getIncomeByDate params:param parser:parser result:^(ParserResult *myResult) {
        [CustomLoadingView hideHudForView:self.view];
        if (myResult.code==0)
        {
            self.incomeInfo = (HBGetIncomeByDateInfo *)myResult.result;
            [self reloadData];
            
            
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        [CustomLoadingView hideHudForView:self.view];
        DISSMISS_ERR([error description]);
    }];

}

@end
